const path = require('path');

const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopywebpackPlugin = require('copy-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin')

// 添加cesium模块路径
const cesiumSource = 'node_modules/cesium/Source';
const cesiumWorkers = 'node_modules/cesium/Build/Cesium';


module.exports = {
    mode : 'development',
    //mode : 'production',
    context: __dirname,
    entry: {
        app: './src/index.js'
    },
    output: {
        filename: 'OneEarth.js',
        path: path.resolve(__dirname, 'dist'),
        library: {
            name : 'OneEarth',
            type : 'umd'
        }
    },    
    module: {
        /* 添加css和静态资源load规则 */
        rules: [{
            test: /\.css$/,
            use: [ 'style-loader', 'css-loader' ]
        }, {
            test: /\.(png|gif|jpg|jpeg|svg|xml|json)$/,
            use: [ 'url-loader' ]
        }, {
            test: /\.glsl$/,
            loader: 'webpack-glsl-loader'
        }]
    },
    amd: {        
        // 启用webpack对cesium格式友好，也就是不再对cesium进行格式的严格检查
        toUrlUndefined: true
    },
    node: {
        //解决其他模块使用fs的问题
        // fs: 'empty'
    },
    plugins: [        
        // 添加Cesium的三个静态文件的文件夹到plugins：Assets, Widgets, Workers
        new CopywebpackPlugin({
            patterns: [
                { from: path.join(cesiumWorkers, 'Workers'),to: 'Workers' }
                // { from: path.join(cesiumSource, 'WorkersES6'),to: 'Workers' }
            ],
        }),
        new CopywebpackPlugin({
            patterns: [
                { from: path.join(cesiumSource, 'Assets'),to: 'Assets' }
            ],
        }),
        new CopywebpackPlugin({
            patterns: [
                { from: path.join(cesiumSource, 'Widgets'),to: 'Widgets' }
            ],
        }),
        new CopywebpackPlugin({
            patterns: [
                { from: path.join('./public', 'css'),to: 'css' }
            ],
        }),
        new webpack.DefinePlugin({
            // Define relative base path in cesium for loading assets
            CESIUM_BASE_URL: JSON.stringify('')
        }),
        new CleanWebpackPlugin()
    ],
    resolve: {
        alias: {
            // CesiumJS module name
            cesium: path.resolve(__dirname, cesiumSource)
        },
        fallback:{
            //url库
            "url": require.resolve("url/"),
            //zlib相关库
            "zlib": require.resolve("browserify-zlib"),
            "assert": require.resolve("assert/"),
            "buffer": require.resolve("buffer/"),
            //stream
            "stream": require.resolve("stream-browserify"),
            //https
            "https": require.resolve("https-browserify"),
            //http
            "http": require.resolve("stream-http")
        }
    },
    // externals: [
    //     'cesium/Cesium'
    // ]
};
